﻿// TeskDialog.cpp: 实现文件
//

#include "pch.h"
#include "WeChatTool.h"
#include "TeskDialog.h"
#include "afxdialogex.h"
#include <mutex>
#include "ConBase.h"
#include <direct.h>

//消息结构列表锁
std::mutex g_mutex_rcvpic;

// CTeskDialog 对话框静态指针
CTeskDialog* CTeskDialog::m_TeskDialog;

std::string CReceivePicture::m_sRevPicPath;

IMPLEMENT_DYNAMIC(CTeskDialog, CDialogEx)

CTeskDialog::CTeskDialog(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG1, pParent)
{
	m_TeskDialog = this;
	
}

CTeskDialog::~CTeskDialog()
{
}

void CTeskDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_BUTTON1, m_hookRevieMsg);
	DDX_Control(pDX, IDC_BUTTON2, m_unhookRevieMsg);
	DDX_Control(pDX, IDC_EDIT1, m_editRevieMsg);
	DDX_Control(pDX, IDC_PICTURE, m_revPicture);
	DDX_Control(pDX, IDC_EDIT4, m_revPicPath);
}


BEGIN_MESSAGE_MAP(CTeskDialog, CDialogEx)
	ON_BN_CLICKED(IDC_BUTTON1, &CTeskDialog::OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON2, &CTeskDialog::OnBnClickedButton2)
	ON_BN_CLICKED(IDC_BUTTON3, &CTeskDialog::OnBnClickedButton3)
END_MESSAGE_MAP()


// CTeskDialog 消息处理程序


BOOL CTeskDialog::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	//char* buffer;
	////也可以将buffer作为输出参数
	//if ((buffer = getcwd(NULL, 0)) == NULL)
	//{
	//	MessageBox("getcwd error", "", 0);
	//	return TRUE;
	//}

	////如果文件夹不存在则创建一个目录，在当前dll所在磁盘目录
	//std::string str_url = buffer;
	//std::string prefix = str_url + "\\images";
	//free(buffer);

	CReceivePicture::m_sRevPicPath = "D:\\PhotoTemp";

	m_revPicPath.SetWindowTextA(CReceivePicture::m_sRevPicPath.c_str());

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}


/**
* @ breif 接收消息回调
* @ param pInfo		消息结构参数
*/
void CTeskDialog::GetRecvMsg(pRecvMsg pInfo)
{
	if (pInfo->sType == 1)
	{
		CString strTemp;
		strTemp.Format("%s 说:%s\r\n"
			, pInfo->sSenderId.c_str()
			, pInfo->sDataBuf.c_str()
		);
		m_editRevieMsg.SetWindowTextA(strTemp.GetString());
	}
	else if (pInfo->sType == 3)
	{
		std::lock_guard<std::mutex> locked(g_mutex_rcvpic);
		RecvMsg rMsg;
		rMsg.sFileName = pInfo->sFileName;
		rMsg.sSenderId = pInfo->sSenderId;
		rMsg.sType = pInfo->sType;
		m_vRecvMsg.push_back(rMsg);
	}
}

/**
* @ breif 接收图片回调
* @ param pInfo		消息结构参数
*/
void CTeskDialog::GetRecvPicture(pRecvMsg pInfo)
{
	std::lock_guard<std::mutex> locked(g_mutex_rcvpic);
	bool isFind = false;
	std::string sSenderId;
	for (std::vector<RecvMsg>::iterator i = m_vRecvMsg.begin(); i != m_vRecvMsg.end();)
	{	
		if (::strstr(i->sFileName.c_str(), pInfo->sFileName.c_str()))
		{
			isFind = true;
			sSenderId = i->sSenderId;
			i = m_vRecvMsg.erase(i);
			break;
		}
		else
		{
			++i;
		}
	}
	if (isFind)
	{
		CString strTemp;
		strTemp.Format(" %s发来了一张图片:%s\r\n"
			, sSenderId.c_str()
			, pInfo->sPath.c_str()
		);

		m_editRevieMsg.SetWindowTextA(strTemp.GetString());

		//加载图片
		CBitmap bitmap;  // CBitmap对象，用于加载位图   
		HBITMAP hBmp;    // 保存CBitmap加载的位图的句柄   
		bitmap.LoadBitmapA(pInfo->sPath.c_str());  // 将位图IDB_BITMAP1加载到bitmap   
		hBmp = (HBITMAP)bitmap.GetSafeHandle();  // 获取bitmap加载位图的句柄   
		m_revPicture.SetBitmap(hBmp);    // 设置图片控件m_jzmPicture的位图图片为IDB_BITMAP1   
	}
}

/**
* @ breif 接收消息回调
* @ param pInfo		消息结构参数
*/
void WINAPI CallBackRecvMsg(pRecvMsg pInfo)
{
	__try
	{
		CTeskDialog::m_TeskDialog->GetRecvMsg(pInfo);
	}
	__except (EXCEPTION_EXECUTE_HANDLER)
	{

	}
}


/**
* @ breif 接收图片回调
* @ param pInfo		消息结构参数
*/
void WINAPI CallBackRecvPicture(pRecvMsg pInfo)
{
	__try
	{
		CTeskDialog::m_TeskDialog->GetRecvPicture(pInfo);
	}
	__except (EXCEPTION_EXECUTE_HANDLER)
	{

	}
}
/**
* @ brief HookRecievMsg
*/
void CTeskDialog::OnBnClickedButton1()
{
	// TODO: 在此添加控件通知处理程序代码
	m_Receive.ToHook(CallBackRecvMsg);
	m_ReceivePicture.ToHook(CallBackRecvPicture);
}

/**
* @ brief UnHookRevievMsg
*/
void CTeskDialog::OnBnClickedButton2()
{
	// TODO: 在此添加控件通知处理程序代码
	m_Receive.UnHook();
	m_ReceivePicture.UnHook();
}

//修改m_revPicPath路径
void CTeskDialog::OnBnClickedButton3()
{
	// TODO: 在此添加控件通知处理程序代码
	//选择文件夹	
	std::string _Path, root;
	TCHAR szDir[MAX_PATH];
	BROWSEINFO bi;
	ITEMIDLIST* pidl;
	bi.hwndOwner = this->m_hWnd;
	bi.pidlRoot = NULL;
	bi.pszDisplayName = szDir;//这个是输出缓冲区   
	bi.lpszTitle = _T("文件夹："); //标题  
	bi.ulFlags = BIF_NEWDIALOGSTYLE;//使用新的界面,在win7中效果较好//BIF_RETURNONLYFSDIRS;   
	bi.lpfn = NULL;
	bi.lParam = 0;
	bi.iImage = 0;
	pidl = SHBrowseForFolder(&bi);//弹出对话框   
	if (pidl == NULL)//点了取消，或者选择了无效的文件夹则返回NULL  
		return;

	if (SHGetPathFromIDList(pidl, szDir)) {
		/*int length = WideCharToMultiByte(CP_ACP, 0, szDir, -1, NULL, 0, NULL, NULL);
		char ch[] = "";
		WideCharToMultiByte(CP_ACP, 0, szDir, -1, ch, length, NULL, NULL);*/
		root = std::string(szDir);
	}

	CReceivePicture::m_sRevPicPath = root;

	m_revPicPath.SetWindowTextA(CReceivePicture::m_sRevPicPath.c_str());
}

